package com.sc.nft.entity.vo;

import cn.hutool.core.date.DateTime;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sc.nft.entity.TradingPoolConfig;
import com.sc.nft.enums.TradingPoolStatusEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

@Data
public class TradingPoolDetailsVO {

    @ApiModelProperty(value = "交易池详情")
    private Wrapper wrapper;

    @ApiModelProperty(value = "交易池状态")
    private TradingPoolStatusEnum tradingPoolStatusEnum;

    @Data
    public static class Wrapper {

        @ApiModelProperty(value = "池id")
        private Long id;

        @ApiModelProperty(value = "缩略图")
        private String creationContent;

        @ApiModelProperty(value = "跳转详情")
        private String skipUrl;

        @ApiModelProperty(value = "标题")
        private String firstTitle;

        @ApiModelProperty(value = "系列")
        private String name;

        @ApiModelProperty(value = "总库存")
        private Integer sumCount;

        @ApiModelProperty(value = "当前价格")
        private BigDecimal currentPrice;

        @ApiModelProperty(value = "当前库存")
        private Integer currentStock;

        @ApiModelProperty(value = "今日成交量")
        private Integer dealToday;

        @ApiModelProperty(value = "开始时间")
        @JsonFormat(pattern = "HH:mm:ss", timezone = "GMT+8")
        private DateTime startTime;

        @ApiModelProperty(value = "结束时间")
        @JsonFormat(pattern = "HH:mm:ss", timezone = "GMT+8")
        private DateTime endTime;

        @ApiModelProperty(value = "详情页图类型 1 图片 2:动图 3:3d模型")
        private Integer productDetailPictureType;

        @ApiModelProperty("详情页图")
        private String productDetailPicture;

        @ApiModelProperty(value = "3d模型贴图")
        private String modelImg;

        @ApiModelProperty(value = "系列")
        private String catenaName;

        @ApiModelProperty(value = "3d模型距离")
        private String modelDistance;

        @ApiModelProperty(value = "今日最高")
        private String todaySHighest;

        @ApiModelProperty(value = "今日最低")
        private String todayLowest;

        @ApiModelProperty(value = "发行方名字")
        private String issuerName;

        @ApiModelProperty(value = "发行方头像")
        private String issuerAvatar;

        @ApiModelProperty(value = "规则说明")
        private String rule;

        @ApiModelProperty(value = "作品故事")
        private String workStory;

        public void setTodaySHighest(BigDecimal todaySHighest) {
            this.todaySHighest = todaySHighest.setScale(2,BigDecimal.ROUND_DOWN).toPlainString();
        }

        public void setTodayLowest(BigDecimal todayLowest) {
            this.todayLowest = todayLowest.setScale(2,BigDecimal.ROUND_DOWN).toPlainString();
        }
    }


    public TradingPoolDetailsVO(Wrapper wrapper, TradingPoolConfig tradingPoolConfig) {
        timeJudgment(tradingPoolConfig);
        this.wrapper = wrapper;
    }

    /**
     * 时间的判断
     *
     * @param tradingPoolConfig
     */
    private void timeJudgment(TradingPoolConfig tradingPoolConfig) {
        // 根据时间推算,当前时间如果小于开始时间则是未开始 如果大于结束时间则是已结束,如果status=0的话则是关闭 如果在时间范围内则是正常
        // 转换后的当前时间
        DateTime localityTime = DateTime.of(LocalTime.now().truncatedTo(ChronoUnit.SECONDS).toString(), "HH:mm:ss");
        // 转换配置开始时间
        DateTime startTime = DateTime.of(tradingPoolConfig.getStartTime());
        // 转换配置结束时间
        DateTime endTime = DateTime.of(tradingPoolConfig.getEndTime());
        // 判断localityTime在不在startTime和endTime之间,在的话则是正常
        if (localityTime.isAfter(startTime) && localityTime.isBefore(endTime)) {
            this.tradingPoolStatusEnum = TradingPoolStatusEnum.NORMAL;
        }

        // 如果当前时间小于开始时间
        if (localityTime.isBefore(startTime)) {
            this.tradingPoolStatusEnum = TradingPoolStatusEnum.NOT_YET_STARTED;
        }

        // 如果当前时间大于结束时间
        if (localityTime.isAfter(endTime)) {
            this.tradingPoolStatusEnum = TradingPoolStatusEnum.ALREADY_ENDED;
        }

        // 是否关闭
        if (!tradingPoolConfig.getStatus()) {
            this.tradingPoolStatusEnum = TradingPoolStatusEnum.CLOSED;
        }
    }
}
